/*
* 数据缓存管理
* */
import 'dart:convert';
import 'dart:developer';
import 'package:lighthouse/index.dart';

class DeptCache {
  static const String _keyStr = "dept";

  // 单例公开访问点
  factory DeptCache() => _sharedInstance();

  // 静态私有成员，没有初始化
  static DeptCache? _instance;

  // 私有构造函数
  DeptCache._() {
    // 具体初始化代码
  }

  // 静态、同步、私有访问点
  static DeptCache _sharedInstance() {
    return _instance ??= DeptCache._();
  }

  late bool _isLoadData = false;

  /*当前网络数据*/
  List<Dept>? _netData;

  /*历史缓存数据*/
  List<Dept>? _cacheData;

  /*加载网络数据*/
  Future loadNetData(BuildContext context) async {
    if (!_isLoadData) {
      _isLoadData = true;
      try {
        ResponseData<List<Dept>> responseData =
            await Api(context).getDeptTree();
        _netData = responseData.dataModel;
        //保存数据
        if (responseData.success) {
          Global.getPrefs().setString(_keyStr, jsonEncode(responseData.data));
        }
      } catch (e) {
        Debug.info(e.toString());
        _isLoadData = false;
        if (e is DioError) {
          //登录失败则提示
          showToast(e.response?.statusMessage ?? S.of(context).networkError);
        } else if (e is Error) {
          Debug.info(e.stackTrace.toString());
        }
      } finally {
        // 隐藏loading框
        // Navigator.of(context).pop();
      }
    }
    return _isLoadData;
  }

  /*获取当前数据*/
  List<Dept>? getCurrentData() {
    List<Dept>? mData;
    if (_netData != null) {
      // 如果有请求的数据 就返回请求的数据
      mData = _netData;
    } else if (_cacheData == null) {
      // 设置缓存数据
      var _cacheStr = Global.getPrefs().getString(_keyStr);
      if (_cacheStr != null) {
        try {
          mData = _cacheData =
              jsonDecode(_cacheStr).map<Dept>((e) => Dept.fromJson(e)).toList();
        } on Error catch (e) {
          Debug.info(e.toString());
          Debug.info(e.stackTrace.toString());
        }
      }
    } else {
      mData = _cacheData;
    }
    return mData;
  }

  /*获取当前数据*/
  List<Dept>? getCurrentDeptList() {
    List<Dept>? list = getCurrentData();
    if (list != null) {
      return _deptList(list);
    } else {
      return null;
    }
  }

  List<Dept> _deptList(List<Dept> list) {
    List<Dept> deptList = [];
    for (Dept dept in list) {
      deptList.add(dept);
      if (dept.children != null) {
        deptList.addAll(_deptList(dept.children!));
      }
    }
    return deptList;
  }

  /*获取当前数据*/
  String getDeptName(String? id) {
    if (id != null) {
      List<Dept>? list = getCurrentDeptList();
      if (list != null) {
        for (var o in list) {
          if (o.id == id) {
            return o.title;
          }
        }
      }
      return id;
    }
    return "";
  }

  /*清楚数据*/
  void clearData() {
    _isLoadData = false;
    _netData = null;
    _cacheData = null;
    Global.getPrefs().remove(_keyStr);
  }

  static List<String>? deptListToStringList(List<Dept>? deptList) {
    if (deptList != null && deptList.isNotEmpty) {
      List<String> list = [];
      for (Dept dept in deptList) {
        list.add(dept.title);
      }
      return list;
    } else {
      return null;
    }
  }
}
